package com.xishu.controller;

import com.xishu.entity.shop.ShopMessage;
import com.xishu.response.ResponseData;
import com.xishu.response.ResponseStatus;
import com.xishu.response.VerifyUtil;
import com.xishu.service.CommonService;
import com.xishu.service.ShopMessageService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@RestController
public class ShopMessageController {

    private CommonService commonService = CommonService.getInstance();

    private ShopMessageService shopMessageService = ShopMessageService.getInstance();

    @ApiModelProperty(value = "发送消息")
    @PostMapping(value = ("/shopmessage/send"), produces = ("application/json;charset=UTF-8"))
    public ResponseData register(@RequestBody ShopMessage shopMessage) throws Exception {
        VerifyUtil.isEmpty(shopMessage.getCustomerId(), shopMessage.getShopId());
        shopMessage.setStatus(0);
        return commonService.createObject(shopMessage, null, true, ResponseStatus.NAME_EXISTS);
    }

    @ApiOperation(value = "用户已读消息标记")
    @PutMapping(value = ("/shopmessage/read"), produces = ("application/json;charset=UTF-8"))
    public ResponseData delete(@RequestBody ShopMessage shopMessage) throws Exception {
        VerifyUtil.verify(() -> shopMessage.getId() != null);
        //标记已读
        shopMessage.setStatus(1);
        commonService.updateObject(shopMessage);
        return ResponseData.emptyResponse();
    }

    @ApiOperation(value = "查询用户全部消息")
    @PutMapping(value = ("/shopmessage/search"), produces = ("application/json;charset=UTF-8"))
    public ResponseData searchByCustomerId(@RequestBody ShopMessage shopMessage) throws Exception {
        VerifyUtil.isEmpty(shopMessage.getCustomerId(), shopMessage.getShopId());
        return commonService.searchResponse(shopMessage);
    }

    @ApiOperation(value = "查询用户未读消息")
    @PutMapping(value = ("/shopmessage/search/unread"), produces = ("application/json;charset=UTF-8"))
    public ResponseData searchCustomerUnReadMessage(@RequestBody ShopMessage shopMessage) throws Exception {
        VerifyUtil.isEmpty(shopMessage.getCustomerId(), shopMessage.getShopId());
        shopMessage.setStatus(0);
        return commonService.searchResponse(shopMessage);
    }

    @ApiOperation(value = "消息删除")
    @DeleteMapping(value = ("/shopmessage/delete"), produces = ("application/json;charset=UTF-8"))
    public ResponseData deleteMessage(@RequestBody ShopMessage shopMessage) throws Exception {
        VerifyUtil.verify(() -> shopMessage.getId() != null);
        return commonService.delete(shopMessage, false);
    }


}
